﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;

namespace DNA.Mvc
{
    [
    XmlRoot("widget", Namespace = "http://www.w3.org/ns/widgets"),
    Serializable
    ]
    public class WidgetTemplate
    {
        #region From W3C
        [XmlAttribute("id")]
        public string ID;

        [XmlElement("author")]
        public WidgetAuthor Author;

        [XmlAttribute("version")]
        public string Version;

        [XmlElement("name")]
        public string Name;

        [XmlElement("description")]
        public string Description;

        [XmlElement("icon", Type = typeof(WidgetIcon))]
        public List<WidgetIcon> Icons;
        #endregion

        [XmlElement("defaults", Namespace = "http://www.dotnetage.com/XML/Schema/widget")]
        public WidgetDefaults Defaults;

        [XmlElement("feature", Type = typeof(WidgetFeature))]
        public WidgetFeature Feature;

        [XmlElement("license", Type = typeof(WidgetLicense))]
        public List<WidgetLicense> Licenses;

        [XmlElement("preference", Type = typeof(WidgetUserPreference))]
        public List<WidgetUserPreference> UserPreferences;

        [XmlElement("content", Type = typeof(WidgetContent))]
        public WidgetContent Content;

        [XmlIgnore]
        public DateTime Modified;
        //private string category = "shared";

        //[XmlIgnore]
        //public string Category
        //{
        //    get { return category; }
        //    set { category = value; }
        //}
    }

    [Serializable]
    public struct WidgetDefaults
    {
        [XmlElement("title")]
        public string Title;

        [XmlElement("titleUrl")]
        public string TitleLinkUrl;

        [XmlElement("iconUrl")]
        public string IconUrl;

        [XmlElement("showBorder")]
        public bool ShowBorder;

        [XmlElement("showHeader")]
        public bool ShowHeader;

        [XmlElement("headerStyle")]
        public string HeaderCssText;

        [XmlElement("bodyStyle")]
        public string BodyCssText;

        [XmlElement("style")]
        public string CssText;

        [XmlElement("zoneID")]
        public string ZoneID;

        [XmlElement("pos")]
        public int Position;

        [XmlElement("area")]
        public string Area;

        [XmlElement("action")]
        public string Action;

        [XmlElement("controller")]
        public string Controller;

        //[XmlElement("assembly")]
        //public string PackageAssemblyName;
    }

    [Serializable]
    public struct WidgetLicense
    {
        [XmlText]
        public string Text;

        [XmlAttribute("href")]
        public string Source;
    }

    [Serializable]
    public struct WidgetAuthor
    {
        [XmlText]
        public string Name;

        [XmlAttribute("href")]
        public string Uri;

        [XmlAttribute("email")]
        public string Email;
    }

    [Serializable]
    public struct WidgetIcon
    {
        [XmlAttribute("src")]
        public string Source;

        [XmlAttribute("width")]
        public int Width;

        [XmlAttribute("height")]
        public int Height;
    }

    [Serializable]
    public struct WidgetFeature
    {
        [XmlAttribute("name")]
        public string Url;

        [XmlAttribute("required")]
        public bool IsRequried;

        [XmlElement("param", Type = typeof(WidgetFeatureParam))]
        public List<WidgetFeatureParam> Params;
    }

    [Serializable]
    public struct WidgetContent : IXmlSerializable
    {
        [XmlAttribute("src")]
        public string Source;

        [XmlAttribute("type")]
        public string ContentType;

        [XmlAttribute("encoding")]
        public string Encoding;

        [XmlText]
        public string Text;

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            Source = reader.GetAttribute("src") == null ? "" : reader.GetAttribute("src");
            ContentType = reader.GetAttribute("type") == null ? "" : reader.GetAttribute("type");
            Encoding = reader.GetAttribute("encoding") == null ? "UTF8" : reader.GetAttribute("encoding");
            Text = reader.ReadElementString();
        }

        public void WriteXml(XmlWriter writer)
        {
            if (!string.IsNullOrEmpty(Source))
                writer.WriteAttributeString("src", Source);

            if (!string.IsNullOrEmpty(ContentType))
                writer.WriteAttributeString("type", ContentType);

            if (!string.IsNullOrEmpty(Encoding))
                writer.WriteAttributeString("encoding", Encoding);

            if (!string.IsNullOrEmpty(Text))
                writer.WriteCData(Text);
        }
    }

    [Serializable]
    public struct WidgetFeatureParam
    {
        [XmlAttribute("name")]
        public string Name;

        [XmlAttribute("value")]
        public string Value;
    }

    [Serializable]
    public struct WidgetUserPreference
    {
        [XmlAttribute("name")]
        public string Name;

        [XmlAttribute("type")]
        public string Type;

        [XmlAttribute("readonly")]
        public bool IsReadonly;

        [XmlAttribute("value")]
        public string Value;
    }
}